import Api from '@/services/api.js';
const app = getApp()
export default {
  data() {
    return {
      img: '', // 头像
      nickName: '', // 昵称
    }
  },
  computed: {
    //窗口宽度
    modalWidth() {
      let {
        windowWidth
      } = app.globalData.systemInfo
      return windowWidth - 80;
    },
    //窗口高度
    modalHeight() {
      let {
        windowHeight
      } = app.globalData.systemInfo
      return windowHeight * this.heightPercent
    },
    privacyData() {
      return this.$store.state.privacyData;
    }
  },
  methods: {
    togglePopup(isShow) {
      this.$refs.popup.togglePopup(isShow)
    },
    handleCancel() {
      this.togglePopup(false);
    },
    // 选择头像
    async chooseavatar(e) {
      const { avatarUrl } = e.detail;
      const ret = await this.$upload({ filePath: avatarUrl })
      if (ret.code == 200) {
        this.img = ret.message;
      }
    },
    inputChange(e) {
      this.nickName = e.detail.value;
    },
    // 提交事件
    async handleSubmit() {
      if (!this.img)
        return uni.showToast({ title: '头像不能为空', icon: 'none' })
      if (!this.nickName)
        return uni.showToast({ title: '昵称不能为空', icon: 'none' })
      const ret = await this.$request({
        url: Api.updateUserInfo,
        method: 'POST',
        data: {
          img: this.img,
          nickName: this.nickName
        }
      })
      if (ret.code == 200) {
        // 更新成功之后 保存在本地
        this.$Serv.updatePrivacyData({
          avatarUrl: this.img,
          nickName: this.nickName
        })
        // 关闭弹窗
        this.togglePopup(false);
        uni.showToast({ title: '操作成功', icon: 'none' })
        setTimeout(() => {
          this.$Serv.backTargetPage('/pages/quizzes/index',  'switchTab');
        }, 800)
      }
    },
  }
}